package com.djinfo.work.activiti.dao.impl;

import com.djinfo.work.activiti.dao.UsersDao;
import com.djinfo.work.common.dao.impl.BaseDaoImpl;
import com.djinfo.work.entity.TbSysUsers;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 请假
 */
@Repository("UsersDao")
public class UsersDaoImpl extends BaseDaoImpl implements UsersDao {

    @Override
    public List<TbSysUsers> getallTbSysUsers(int pagenum, int pagesize) {
        StringBuffer sql = new StringBuffer();
        sql.append("select  * from tb_sys_user u ");
        sql.append("left join tb_sys_role_user  ur on u.pk_user_id=ur.user_id ");
        sql.append("left join tb_sys_role r on ur.role_id=r.pk_role_id ");
        sql.append("WHERE 1=1");
        SQLQuery sQLQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql.toString());
        sQLQuery.addEntity("a", TbSysUsers.class);
        sQLQuery.setFirstResult((pagenum - 1) * pagesize).setMaxResults(
                pagesize);
        List<TbSysUsers> tbSysUsersList = sQLQuery.list();
        removeDuplicate(tbSysUsersList);
        return tbSysUsersList;
    }
    public  void removeDuplicate(List arlList)
    {
        HashSet h = new HashSet(arlList);
        arlList.clear();
        arlList.addAll(h);
    }
    @Override
    public TbSysUsers getTbSysUsersbyid(String id) {
        return (TbSysUsers)this.getByPrimaryKey(TbSysUsers.class,id);
    }

    @Override
    public String getUidByusername(String username) {
        String hql ="from TbSysUsers where username='"+username+"'";
        TbSysUsers tbSysUsers = (TbSysUsers) this.getUniqueObject(hql);
        String id = tbSysUsers.getPkUserId();
        return id;
    }

    @Override
    public List<TbSysUsers> getTbSysUsers() {
        StringBuffer sql = new StringBuffer();
        sql.append("select  * from tb_sys_user u ");
        sql.append("left join tb_sys_role_user  ur on u.pk_user_id=ur.user_id ");
        sql.append("left join tb_sys_role r on ur.role_id=r.pk_role_id ");
        sql.append("WHERE 1=1");
        SQLQuery sQLQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql.toString());
        sQLQuery.addEntity("a", TbSysUsers.class);
        List<TbSysUsers> tbSysUsersList = sQLQuery.list();
        removeDuplicate(tbSysUsersList);
        return tbSysUsersList;
    }
}